package com.project.lvshi.controller.aiSeek;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.lvshi.domain.AiSeekItem;
import com.project.lvshi.domain.Result;
import com.project.lvshi.domain.SysSeekItem;
import com.project.lvshi.domain.User;
import com.project.lvshi.enums.ResultCode;
import com.project.lvshi.service.AiSeekItemService;
import com.project.lvshi.service.SysSeekItemService;
import com.project.lvshi.service.UserService;
import com.project.lvshi.utils.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 咨询内容controller
 * @date 2025/09/27 09:18
 */
@Controller
@ResponseBody
@RequestMapping("aiItem")
public class aiSeekItemController {

    @Autowired
    private AiSeekItemService sysSeekItemService;
    @Autowired
    private UserService userService;

    /** 获取咨询内容 */
    @PostMapping("getAiSeekItemList")
    public Result getSysSeekItemList(@RequestBody AiSeekItem aiSeekItem) {
        QueryWrapper<AiSeekItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(aiSeekItem.getSeekId()),AiSeekItem::getSeekId,aiSeekItem.getSeekId());
        List<AiSeekItem> itemList = sysSeekItemService.list(queryWrapper);
        return Result.success(itemList);
    }

    /** 根据id获取咨询内容 */
    @GetMapping("getAiSeekItemById")
    public Result getSysSeekItemById(@RequestParam("id")String id) {
        AiSeekItem sysSeekItem = sysSeekItemService.getById(id);
        return Result.success(sysSeekItem);
    }

    /** 保存咨询内容 */
    @PostMapping("saveAiSeekItem")
    public Result saveSysSeekItem(@RequestBody AiSeekItem sysSeekItem) {
        String id = TokenUtils.getUserIdByToken();
        User user = userService.getById(id);
        sysSeekItem.setUserId(id);
        sysSeekItem.setAvatar(user.getAvatar());
        boolean save = sysSeekItemService.save(sysSeekItem);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑咨询内容 */
    @PostMapping("editAiSeekItem")
    public Result editSysSeekItem(@RequestBody AiSeekItem sysSeekItem) {
        boolean save = sysSeekItemService.updateById(sysSeekItem);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除咨询内容 */
    @GetMapping("removeAiSeekItem")
    public Result removeSysSeekItem(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                sysSeekItemService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("咨询内容id不能为空！");
        }
    }

}
